<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

<div>
  <button onclick="testGet()">GET</button>
  <button onclick="testPost()">POST</button>
  <button onclick="testPut()">PUT</button>
  <button onclick="testDelete()">DELETE</button>
</div>

<script src="https://cdn.bootcss.com/axios/0.19.0/axios.js"></script>
<script>
  /* 1. GET请求: 从服务器端获取数据*/
  function testGet() {
    // axios.get('http://localhost:3000/comments') // 获取所有posts的数组
    axios.get('http://localhost:3000/comments/1') // 获取id为1的数组
    // axios.get('http://localhost:3000/comments?id=1&id=2') // 获取id为1或2的数组
  }

  /* 2. POST请求: 向服务器端添加新数据*/
  function testPost() {
    axios.post('http://localhost:3000/comments', {body: 'xxx', postId: 1}) // 保存数据
  }

  /* 3. PUT请求: 更新服务器端已经数据 */
  function testPut() {
    axios.put('http://localhost:3000/comments/2', {body: 'yyy', postId: 1})
  }

  /* 4. DELETE请求: 删除服务器端数据 */
  function testDelete() {
    axios.delete('http://localhost:3000/comments/2')
  }
</script>
</body>
</html>